home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue26 / keytrap / DCompfrm.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-07-25  |  7.8 KB  |  347 lines

  1. unit dcompfrm;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, Windows, Messages, Classes, Controls, Forms,
  7.   Dialogs, Menus, ShellAPI, StdCtrls, Buttons, ExtCtrls, Grids, ComCtrls;
  8.  
  9. const
  10.   wm_IconMessage = wm_User;
  11.  
  12. type
  13.   TDCompForm = class(TForm)
  14.     PopupMenu1: TPopupMenu;
  15.     Options1: TMenuItem;
  16.     About1: TMenuItem;
  17.     N1: TMenuItem;
  18.     Close1: TMenuItem;
  19.     BitBtn1: TBitBtn;
  20.     BitBtn2: TBitBtn;
  21.     Panel1: TPanel;
  22.     KeyListBox: TListBox;
  23.     HeaderControl1: THeaderControl;
  24.     FontList: TComboBox;
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure FormDestroy(Sender: TObject);
  27.     procedure Options1Click(Sender: TObject);
  28.     procedure About1Click(Sender: TObject);
  29.     procedure Close1Click(Sender: TObject);
  30.     procedure FormShow(Sender: TObject);
  31.     procedure FontListChange(Sender: TObject);
  32.   private
  33.     NotifyIcon: TNotifyIconData;
  34.     CharList : TStringList;
  35.     procedure AppOnMessage(var Msg: TMsg; var Handled: Boolean);
  36.     procedure TranslateKeys(First,Second : word;Wnd:THandle);
  37.     procedure InitDefaultList;
  38.   public
  39.     procedure IconTray(var Msg: TMessage); message wm_IconMessage;
  40.   end;
  41.  
  42. var
  43.   DCompForm: TDCompForm;
  44.   WM_ToggleIcon,
  45.   WM_TranslateKeys : Cardinal;
  46.  
  47. implementation
  48.  
  49. procedure EnableHook; stdcall; external 'KeyHook.dll';
  50. procedure DisableHook; stdcall; external 'KeyHook.dll';
  51.  
  52. {$R *.DFM}
  53. {$R DComp.res}
  54.  
  55. procedure TDCompForm.FormCreate(Sender: TObject);
  56. begin
  57.   { load the initial icon }
  58.   Icon.Handle := LoadIcon(HInstance, 'LETTERRED');
  59.  
  60.   { fill the NotifyIcon data structure }
  61.   with NotifyIcon do begin
  62.     cbSize := sizeof(NotifyIcon);
  63.     wnd := Handle;
  64.     uID := 1; { icon ID }
  65.     uCallBackMessage := wm_IconMessage;
  66.     hIcon := Icon.Handle;
  67.     szTip := 'DCompose';
  68.     uFlags := nif_Message or nif_Icon or nif_Tip;
  69.   end;
  70.   Shell_NotifyIcon(NIM_ADD, @NotifyIcon);
  71.  
  72.   CharList := TStringList.Create;
  73.   with CharList do begin
  74.     { sort list alphabetically }
  75.     Sorted := true;
  76.     { must set this to accept duplicates; otherwise it seems
  77.       to see the lower and upper case strings as duplicates and
  78.       eliminates lower case }
  79.     Duplicates := dupAccept;
  80.   end;
  81.   InitDefaultList;
  82.  
  83.   Application.OnMessage := AppOnMessage;
  84.   EnableHook;
  85. end;
  86.  
  87. procedure TDCompForm.FormDestroy(Sender: TObject);
  88. begin
  89.   NotifyIcon.uFlags := 0;
  90.   Shell_NotifyIcon (NIM_DELETE, @NotifyIcon);
  91.  
  92.   CharList.Free;
  93.  
  94.   DisableHook;
  95. end;
  96.  
  97. procedure TDCompForm.InitDefaultList;
  98. begin
  99.   with CharList do begin
  100.     Clear;
  101.  
  102.     {0127-0140}
  103. //    
  104. //    Ç
  105. //    ü
  106. //    é
  107.     Add('â'+#9+'f,');
  108. //    ä
  109.     Add('à'+#9+'..');
  110.     Add('å'+#9+'/-');
  111.     Add('ç'+#9+'/=');
  112. //    ê
  113.     Add('ë'+#9+'00');
  114.     Add('è'+#9+'S^');
  115. //    ï
  116.     Add('î'+#9+'OE');
  117.  
  118.     {0141-0150}
  119. //    ì
  120. //    Ä
  121. //    Å
  122. //    É
  123. //    æ
  124. //    Æ
  125.     Add('ô'+#9+'oq');
  126.     Add('ö'+#9+'cq');
  127.     Add('ò'+#9+'bu');
  128. //    û
  129.  
  130.     {0151-0160}
  131. //    ù
  132. //    ÿ
  133.     Add('Ö'+#9+'tm');
  134.     Add('Ü'+#9+'s^');
  135. //    ¢
  136.     Add('£'+#9+'oe');
  137. //    ¥
  138. //    ₧
  139.     Add('ƒ'+#9+'Y"');
  140.  
  141.     {0161-0170}
  142.     Add('í'+#9+'!!');
  143.     Add('ó'+#9+'c|');
  144.     Add('ó'+#9+'c/');
  145.     Add('ú'+#9+'L-');
  146.     Add('ñ'+#9+'xo');
  147.     Add('Ñ'+#9+'Y=');
  148.     Add('ª'+#9+'||');
  149.     Add('º'+#9+'so');
  150.     Add('¿'+#9+'""');
  151.     Add('⌐'+#9+'co');
  152.     Add('¬'+#9+'A_');
  153.  
  154.     {0171-0180}
  155.     Add('½'+#9+'<<');
  156.     Add('¼'+#9+'-,');
  157. //    ¡
  158.     Add('«'+#9+'ro');
  159.     Add('»'+#9+'__');
  160.     Add('░'+#9+'0_');
  161.     Add('▒'+#9+'+-');
  162.     Add('▓'+#9+'2_');
  163.     Add('│'+#9+'3_');
  164.     Add('Æ'+#9+'''''');
  165.  
  166.     {0181-0190}
  167.     Add('╡'+#9+'/U');
  168.     Add('╢'+#9+'P!');
  169.     Add('╖'+#9+'._');
  170.     Add('╕'+#9+',,');
  171.     Add('╣'+#9+'1_');
  172.     Add('║'+#9+'O_');
  173.     Add('╗'+#9+'>>');
  174.     Add('╝'+#9+'14');
  175.     Add('╜'+#9+'12');
  176.     Add('╛'+#9+'34');
  177.  
  178.     {0191-0200}
  179.     Add('┐'+#9+'??');
  180.     Add('└'+#9+'A`');
  181.     Add('┴'+#9+'A''');
  182.     Add('┬'+#9+'A^');
  183.     Add('├'+#9+'A~');
  184.     Add('─'+#9+'A"');
  185.     Add('┼'+#9+'Ao');
  186.     Add('╞'+#9+'AE');
  187.     Add('╟'+#9+'C,');
  188.     Add('╚'+#9+'E`');
  189.  
  190.     {0201-0210}
  191.     Add('╔'+#9+'E''');
  192.     Add('╩'+#9+'E^');
  193.     Add('╦'+#9+'E"');
  194.     Add('╠'+#9+'I`');
  195.     Add('═'+#9+'I''');
  196.     Add('╬'+#9+'I^');
  197.     Add('╧'+#9+'I"');
  198.     Add('╨'+#9+'D-');
  199.     Add('╤'+#9+'N~');
  200.     Add('╥'+#9+'O`');
  201.  
  202.     {0211-0220}
  203.     Add('╙'+#9+'O''');
  204.     Add('╘'+#9+'O^');
  205.     Add('╒'+#9+'O~');
  206.     Add('╓'+#9+'O"');
  207.     Add('╫'+#9+'xx');
  208.     Add('╪'+#9+'O/');
  209.     Add('┘'+#9+'U`');
  210.     Add('┌'+#9+'U''');
  211.     Add('█'+#9+'U^');
  212.     Add('▄'+#9+'U"');
  213.  
  214.     {0221-0230}
  215.     Add('▌'+#9+'Y''');
  216.     Add('▐'+#9+'TH');
  217.     Add('▀'+#9+'ss');
  218.     Add('α'+#9+'a`');
  219.     Add('ß'+#9+'a''');
  220.     Add('Γ'+#9+'a^');
  221.     Add('π'+#9+'a~');
  222.     Add('Σ'+#9+'a"');
  223.     Add('σ'+#9+'ao');
  224.     Add('µ'+#9+'ae');
  225.  
  226.     {0231-0240}
  227.     Add('τ'+#9+'c,');
  228.     Add('Φ'+#9+'e`');
  229.     Add('Θ'+#9+'e''');
  230.     Add('Ω'+#9+'e^');
  231.     Add('δ'+#9+'e"');
  232.     Add('∞'+#9+'i`');
  233.     Add('φ'+#9+'i''');
  234.     Add('ε'+#9+'i^');
  235.     Add('∩'+#9+'i"');
  236.     Add('≡'+#9+'d-');
  237.  
  238.     {0241-0250}
  239.     Add('±'+#9+'n~');
  240.     Add('≥'+#9+'o`');
  241.     Add('≤'+#9+'o''');
  242.     Add('⌠'+#9+'o^');
  243.     Add('⌡'+#9+'o~');
  244.     Add('÷'+#9+'o"');
  245.     Add('≈'+#9+'-:');
  246.     Add('°'+#9+'o/');
  247.     Add('∙'+#9+'u`');
  248.     Add('·'+#9+'u''');
  249.  
  250.     {0251-0255}
  251.     Add('√'+#9+'u^');
  252.     Add('ⁿ'+#9+'u"');
  253.     Add('²'+#9+'y''');
  254.     Add('■'+#9+'th');
  255.     Add(' '+#9+'y"');
  256.   end;
  257. end;
  258.  
  259. procedure TDCompForm.IconTray(var Msg: TMessage);
  260. var
  261.   Pt: TPoint;
  262. begin
  263.   if Msg.lParam = wm_rbuttondown then begin
  264.     GetCursorPos(Pt);
  265.     SetForegroundWindow(Handle);
  266.     PopupMenu1.Popup(Pt.x, Pt.y);
  267.   end;
  268.   if Msg.lParam = wm_lbuttondblclk then
  269.     Options1Click(self);
  270. end;
  271.  
  272. procedure TDCompForm.AppOnMessage(var Msg: TMsg; var Handled: Boolean);
  273. begin
  274.   if (Msg.hwnd = Handle) or (Msg.hwnd = Application.Handle) then
  275.     if Msg.Message = WM_ToggleIcon then begin
  276.       if Msg.WParam = 1 then begin
  277.         NotifyIcon.hIcon := LoadIcon(HInstance, 'LETTERGREEN');
  278.       end
  279.       else begin
  280.         NotifyIcon.hIcon := LoadIcon(HInstance, 'LETTERRED');
  281.       end;
  282.       Shell_NotifyIcon (NIM_MODIFY, @NotifyIcon);
  283.       Handled := true;
  284.     end
  285.     else if Msg.Message = WM_TranslateKeys then begin
  286.       TranslateKeys(LoWord(Msg.wParam),HiWord(Msg.wParam),Msg.lParam);
  287.       Handled := true;
  288.     end;
  289. end;
  290.  
  291. procedure TDCompForm.TranslateKeys(First,Second : word;Wnd:THandle);
  292. var
  293.   i,AccChar : word;
  294. begin
  295.   AccChar := 0;
  296.   for i := 0 to pred(CharList.Count) {MaxPairs }do begin
  297.     if (CharList[i][3] = char(First)) and (CharList[i][4] = char(Second)) or
  298.        (CharList[i][4] = char(First)) and (CharList[i][3] = char(Second)) then
  299.       AccChar := ord(CharList[i][1]);
  300.   end;
  301.  
  302.   if AccChar = 0 then begin
  303.     PostMessage(Wnd,WM_Char,First,1);
  304.     PostMessage(Wnd,WM_Char,Second,1);
  305.     MessageBeep(-1);
  306.   end
  307.   else
  308.     PostMessage(Wnd,WM_Char,AccChar,1);
  309. end;
  310.  
  311. procedure TDCompForm.Options1Click(Sender: TObject);
  312. begin
  313.   if not Visible then ShowModal;
  314. end;
  315.  
  316. procedure TDCompForm.About1Click(Sender: TObject);
  317. begin
  318.   MessageDlg('DCompose'#13+'Copyright Warren Kovach 1997',
  319.     mtInformation, [mbOk], 0);
  320. end;
  321.  
  322. procedure TDCompForm.Close1Click(Sender: TObject);
  323. begin
  324.   Close;
  325. end;
  326.  
  327. procedure TDCompForm.FormShow(Sender: TObject);
  328. begin
  329.   KeyListBox.Items := CharList;
  330.   FontList.Items := Screen.Fonts;
  331.   FontList.Text := KeyListBox.Font.Name;
  332. end;
  333.  
  334. procedure TDCompForm.FontListChange(Sender: TObject);
  335. begin
  336.   KeyListBox.Font.Name := FontList.Text;
  337. end;
  338.  
  339. initialization
  340.   { must register a unique message ID, both here and in DLL,
  341.     so there is no conflict with other programs }
  342.   WM_ToggleIcon := RegisterWindowMessage('DCompose ToggleIcon');
  343.   WM_TranslateKeys := RegisterWindowMessage('DCompose TranslateKeys');
  344. end.
  345.  
  346.  
  347.